﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.RuneMaster.Core;

namespace Studio77.RuneMaster.Pages
{
    public partial class ModeSelectPage : PhoneApplicationPage
    {
        public ModeSelectPage()
        {
            InitializeComponent();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //清除页面堆栈
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch
            { }

            RefreshGameModeUnlockStatus();

            //根据模式解锁状态来显示
            if (StageManager.Instance.ModeUnlockStatus.ContainsKey("e") && StageManager.Instance.ModeUnlockStatus["e"])
            {
                this.btnEasy.IsEnabled = true;
            }
            else
            {
                this.btnEasy.IsEnabled = false;
            }

            if (StageManager.Instance.ModeUnlockStatus.ContainsKey("n") && StageManager.Instance.ModeUnlockStatus["n"])
            {
                this.btnNormal.IsEnabled = true;
            }
            else
            {
                this.btnNormal.IsEnabled = false;
            }

            if (StageManager.Instance.ModeUnlockStatus.ContainsKey("h") && StageManager.Instance.ModeUnlockStatus["h"])
            {
                this.btnHard.IsEnabled = true;
            }
            else
            {
                this.btnHard.IsEnabled = false;
            }
        }

        /// <summary>
        /// 刷新游戏模式是否被解锁
        /// </summary>
        private void RefreshGameModeUnlockStatus()
        {
            if (!(StageManager.Instance.ModeUnlockStatus.ContainsKey("n") && StageManager.Instance.ModeUnlockStatus["n"]))
            {
                string gameMode = "e";
                int count = 0;
                //查看简单模式下的解锁情况
                for (int i = 1; i <= 10; i++)
                {
                    if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + i)
                            && StageManager.Instance.StageUnlockStatus[gameMode + "_" + i])
                    {
                        count++;
                    }
                }
                if (count >= 6)
                {
                    StageManager.Instance.ModeUnlockStatus["n"] = true;
                    StageManager.Instance.SaveModeUnlockInfo();
                    ////解锁一个关卡
                    //if (!StageManager.Instance.StageUnlockStatus.ContainsKey("n_1"))
                    //{
                    //    StageManager.Instance.StageUnlockStatus.Add("n_1", true);
                    //    StageManager.Instance.SaveStageUnlockInfo();
                    //}
                }
            }

            if (!(StageManager.Instance.ModeUnlockStatus.ContainsKey("h") && StageManager.Instance.ModeUnlockStatus["h"]))
            {
                string gameMode = "n";
                int count = 0;
                //查看简单模式下的解锁情况
                for (int i = 1; i <= 10; i++)
                {
                    if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + i)
                            && StageManager.Instance.StageUnlockStatus[gameMode + "_" + i])
                    {
                        count++;
                    }
                }
                if (count >= 6)
                {
                    StageManager.Instance.ModeUnlockStatus["h"] = true;
                    StageManager.Instance.SaveModeUnlockInfo();

                    ////解锁一个关卡
                    //if (!StageManager.Instance.StageUnlockStatus.ContainsKey("h_1"))
                    //{
                    //    StageManager.Instance.StageUnlockStatus.Add("h_1", true);
                    //    StageManager.Instance.SaveStageUnlockInfo();
                    //}
                }
            }
        }

        private void btnEasy_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            this.NavigationService.Navigate(new Uri("/Pages/StagePage.xaml?Mode=e", UriKind.RelativeOrAbsolute));
        }

        private void btnNormal_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            this.NavigationService.Navigate(new Uri("/Pages/StagePage.xaml?Mode=n", UriKind.RelativeOrAbsolute));
        }

        private void btnHard_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            this.NavigationService.Navigate(new Uri("/Pages/StagePage.xaml?Mode=h", UriKind.RelativeOrAbsolute));
        }
    }
}